package com.yiquan.search.service;

import com.yiquan.search.dao.es.BrowseActionDao;
import com.yiquan.search.dao.es.EsVideoDao;
import com.yiquan.search.dao.es.SearchActionDao;
import com.yiquan.search.entity.BrowseAction;
import com.yiquan.search.entity.SearchAction;
import com.yiquan.search.entity.Video;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.net.ntp.TimeStamp;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import util.IdWorker;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author Tyrone
 * @date 2022/3/7 20:37
 */
@Service
@Slf4j
public class CollectService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private BrowseActionDao browseActionDao;

    @Autowired
    private SearchActionDao searchActionDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private EsVideoDao esVideoDao;

    @Autowired
    private IdWorker idWorker;

    public void collectionBrowseAction(BrowseAction browseAction) {
        Assert.notNull(browseAction, "浏览行为不能为Null");
        browseActionDao.save(browseAction);
        log.info("用户：{} 浏览行为上报成功", browseAction.getUserId());
    }

    public void collectionSearchAction(SearchAction searchAction){
        Assert.notNull(searchAction,"搜索行为不能为Null");
        searchActionDao.save(searchAction);
        log.info("用户：{} 搜索行为上报成功", searchAction.getUserId());
    }

    public void test() {
        for (int i = 0; i < 1000; i++) {
            BrowseAction browseAction = new BrowseAction();

            browseAction.setBrowseDate(System.currentTimeMillis());
            browseAction.setScore(Double.valueOf(RandomUtils.nextInt(1,10)));

            browseAction.setUserId(RandomUtils.nextLong(1,10));
            browseAction.setVideoId(RandomUtils.nextLong(1000,1099));
            browseAction.setVideoIdType(1);
            browseAction.setId(idWorker.nextId());
            mongoTemplate.save(browseAction);
        }
    }

    public void uploadVideoInfo(Video video) {
        Assert.notNull(video, "video不能为Null");
        esVideoDao.save(video);
    }
}
